// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Boss Fireball Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 67 : TYPE_BOSSFIREBALL

// Function declarations
reserve function BossFireball_Function153

// Static Values

// Tables

function BossFireball_Function153
	object.frame = object.animationTimer
	object.frame /= 12
	object.direction = object.animationTimer
	object.direction /= 6
	object.direction &= FLIP_X
	object.direction += object.value1
	object.animationTimer++
	object.animationTimer %= 24
end function


event ObjectMain
	switch object.state
	case 0
		object.value1 = 2
		object.value0++
		if object.value0 == 30
			object.value0 = 0
			object.value1 = 0
			object.state++
			PlaySfx(SfxName[Small Fireball], 0)
		end if
		CallFunction(BossFireball_Function153)
		break
	case 1
		object.yvel += 0x1800
		object.ypos += object.yvel
		if object.yvel > 0x30000
			object.drawOrder = 5
		end if
		ObjectTileCollision(CSIDE_FLOOR, 0, 4, 0)
		if checkResult == 1
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].value3 = object.xpos
			object[tempObjectPos].value3 -= 0x100000
			object[tempObjectPos].xvel = -0xA000
			object[tempObjectPos].state = 3
			object[tempObjectPos].drawOrder = 5
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].value3 = object.xpos
			object[tempObjectPos].value3 += 0x100000
			object[tempObjectPos].xvel = 0xA000
			object[tempObjectPos].state = 3
			object[tempObjectPos].drawOrder = 5
			object.state++
		end if
		CallFunction(BossFireball_Function153)
		break
	case 2
		object.value0++
		if object.value0 == 120
			object.value0 = 0
			object.state = 7
			object.frame = 2
			object.direction = FLIP_NONE
		else
			CallFunction(BossFireball_Function153)
		end if
		break
	case 3
		object.xpos += object.xvel
		if object.xvel > 0
			if object.xpos >= object.value3
				object.xvel = 0
				object.xpos = object.value3
				object.state = 2
				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].value3 = object.xpos
				object[tempObjectPos].value3 += 0x100000
				object[tempObjectPos].xvel = 0xA000
				object[tempObjectPos].state = 4
				object[tempObjectPos].drawOrder = 5
			end if
		else
			if object.xpos <= object.value3
				object.xvel = 0
				object.xpos = object.value3
				object.state = 2
				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].value3 = object.xpos
				object[tempObjectPos].value3 -= 0x100000
				object[tempObjectPos].xvel = -0xA000
				object[tempObjectPos].state = 4
				object[tempObjectPos].drawOrder = 5
			end if
		end if
		CallFunction(BossFireball_Function153)
		break
	case 4
		object.xpos += object.xvel
		if object.xvel > 0
			if object.xpos >= object.value3
				object.xpos = object.value3
				object.state = 5
				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].xvel = 0xA000
				object[tempObjectPos].state = 6
				object[tempObjectPos].drawOrder = 5
			end if
		else
			if object.xpos <= object.value3
				object.xpos = object.value3
				object.state = 5
				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].xvel = -0xA000
				object[tempObjectPos].state = 6
				object[tempObjectPos].drawOrder = 5
			end if
		end if
		CallFunction(BossFireball_Function153)
		break
	case 5
		object.value0++
		temp0 = object.value0
		temp0 %= 48
		if temp0 == 0
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = object.xvel
			object[tempObjectPos].state = 6
			object[tempObjectPos].drawOrder = 5
		end if
		if object.value0 == 120
			object.value0 = 0
			object.state = 7
			object.frame = 2
			object.direction = FLIP_NONE
		else
			CallFunction(BossFireball_Function153)
		end if
		break
	case 6
		object.xpos += object.xvel
		if object.gravity == GRAVITY_GROUND
			ObjectTileGrip(CSIDE_FLOOR, 0, 4, 0)
			object.gravity = checkResult
			object.gravity ^= GRAVITY_AIR
		else
			object.drawOrder = 4
			object.yvel += 0x1800
			object.ypos += object.yvel
		end if
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		object.value0++
		if object.value0 == 90
			object.type = TypeName[Blank Object]
		end if
		CallFunction(BossFireball_Function153)
		break
	case 7
		object.value0++
		if object.value0 == 8
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state > 0
		if object.frame < 2
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					CallFunction(PlayerObject_FireHit)
				end if
			next
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-7, -23, 15, 31, 52, 114)
	SpriteFrame(-8, -24, 16, 32, 68, 114)
	SpriteFrame(-8, -19, 16, 23, 85, 114)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
